package principal;

import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Frame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtNombre;
	private JTextField txtApellido;
	private JTextField txtDni;
	private JLabel lblIng;
	private JPanel panel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frame frame = new Frame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Frame() {
		setType(Type.UTILITY);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 538, 176);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		lblIng = new JLabel("Ingrese los datos correspondientes");
		lblIng.setHorizontalAlignment(SwingConstants.CENTER);
		lblIng.setToolTipText("");
		lblIng.setFont(new Font("Times New Roman", Font.ITALIC, 16));
		
		panel = new JPanel();
		panel.setBorder(new CompoundBorder());
		
		JPanel panelBoton = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblIng, GroupLayout.PREFERRED_SIZE, 492, Short.MAX_VALUE)
					.addContainerGap())
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(156)
					.addComponent(panelBoton, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
					.addGap(163))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblIng, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panelBoton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(32, Short.MAX_VALUE))
		);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
	
			}
		});
		panelBoton.add(btnAceptar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(DISPOSE_ON_CLOSE);
			}
		});
		panelBoton.add(btnCancelar);
		
		JLabel lblNombre = new JLabel("Nombre: ");
		lblNombre.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 12));
		panel.add(lblNombre);
		
		txtNombre = new JTextField();
		txtNombre.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				PrimeraMayus(txtNombre);
			}
		});
		panel.add(txtNombre);
		txtNombre.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				
				char c = e.getKeyChar();
				
				if (!(Character.isLetter(c)) && c != 32 && !(Character.isISOControl(c))) {
					e.consume();
					return;
				}
				
				if (txtNombre.getText().length() == 0 && c == 32) {
					e.consume();
					return;
				}
	
				if (txtNombre.getText().length() > 0) {
				
					if (txtNombre.getText().charAt(txtNombre.getText().length() -1) == 32 && c == 32) {
						e.consume();
						return;
					}				
			}
			}
		});
		
		txtNombre.setColumns(10);
		
		JLabel lblApellido = new JLabel("Apellido: ");
		lblApellido.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 12));
		panel.add(lblApellido);
		
		txtApellido = new JTextField();
		txtApellido.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				PrimeraMayus(txtApellido);
			}
		});
		panel.add(txtApellido);
		txtApellido.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				
				char c = e.getKeyChar();
				
				if (!(Character.isLetter(c)) && c != 32 && !(Character.isISOControl(c))) {
					e.consume();
					return;
				}
				
				if (txtApellido.getText().length() == 0 && c == 32) {
					e.consume();
					return;
				}
	
				if (txtApellido.getText().length() > 0) {
				
					if (txtApellido.getText().charAt(txtApellido.getText().length() -1) == 32 && c == 32) {
						e.consume();
						return;
					}				
			}
				
			}
		});
		txtApellido.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("D.N.I.: ");
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 12));
		panel.add(lblNewLabel);
		
		txtDni = new JTextField();
		txtDni.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				SNumeros(txtDni);
			}
		});
		panel.add(txtDni);
		txtDni.setColumns(10);
		contentPane.setLayout(gl_contentPane);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtNombre, txtApellido, txtDni}));
	}


public void SNumeros(JTextField a) {
	a.addKeyListener(new KeyAdapter() {
		public void keyTyped(KeyEvent e){
			char c = e.getKeyChar();
			
			if (!(Character.isDigit((c)) && !(Character.isISOControl(c)))) {
				e.consume();
				return;
			}
		
		}
		
	});
}

public void PrimeraMayus(JTextField texto){
	String Caracteres = texto.getText();
	
	if (Caracteres.length() > 0) {
		String palabras[] = Caracteres.split(" ");
		String cadena = "";
		
		for (int i = 0; i < palabras.length; i++) {
			palabras[i] = palabras[i].substring(0,1).toUpperCase() + palabras[i].substring(1).toLowerCase();
			cadena = cadena+ palabras[i] +" ";
		}
		
		cadena.trim();
		texto.setText(cadena);				
	} 
}
}